package com.jinzhi.jzweb.controller;


import java.math.BigDecimal;
import java.util.*;

import com.jinzhi.common.utils.ApiResult;
import com.jinzhi.jzweb.domain.*;
import com.jinzhi.jzweb.service.ReserveSpecificationInventoryService;
import com.jinzhi.jzweb.service.ReserveStoreCommodityService;
import com.jinzhi.jzweb.service.impl.ReserveStoreCommodityServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.service.ReserveUserOrderDetailsService;
import com.jinzhi.common.utils.Result;

/**
 * 
 * <pre>
 * 预定商品用户订单表
 * </pre>
 * <small> 2023-08-21 17:05:55 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/reserveUserOrderDetails")
public class ReserveUserOrderDetailsController extends BaseController {
	@Autowired
	private ReserveUserOrderDetailsService reserveUserOrderDetailsService;

	@Autowired
	private ReserveSpecificationInventoryService reserveSpecificationInventoryService; //规格

	@Autowired
	private ReserveStoreCommodityService reserveStoreCommodityService; //商品

	@GetMapping()
	String ReserveUserOrderDetails(){
	    return "jzweb/reserveUserOrderDetails/reserveUserOrderDetails";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public Result<Page<ReserveUserOrderDetailsDO>> list(ReserveUserOrderDetailsDO reserveUserOrderDetailsDTO){
		// 查询列表数据
		Wrapper<ReserveUserOrderDetailsDO> wrapper = new EntityWrapper<ReserveUserOrderDetailsDO>().orderBy("id", false);
		if (reserveUserOrderDetailsDTO.getOrderId() != null) {
			wrapper.eq("order_id", reserveUserOrderDetailsDTO.getOrderId());
		}
		Page<ReserveUserOrderDetailsDO> page = reserveUserOrderDetailsService.selectPage(getPage(ReserveUserOrderDetailsDO.class),wrapper);
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
				ReserveUserOrderDetailsDO obj = (ReserveUserOrderDetailsDO) page.getRecords().get(i);
			ReserveSpecificationInventoryDO reserveSpecificationInventoryDO = reserveSpecificationInventoryService.selectById(obj.getSpecificationId());
			obj.setSpecificationName(reserveSpecificationInventoryDO.getSpecification());
			ReserveStoreCommodityDO reserveStoreCommodityDO = reserveStoreCommodityService.selectById(obj.getCommodityId());
			obj.setCommodityName(reserveStoreCommodityDO.getCommodityName());
			obj.setPrices(reserveStoreCommodityDO.getCommodityPrices());
			obj.setPocket(reserveStoreCommodityDO.getCommodityPrices().doubleValue() * obj.getQuantity());
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}




	@GetMapping("/add")
	@RequiresPermissions("jzweb:reserveUserOrderDetails:add")
	String add(){
	    return "jzweb/reserveUserOrderDetails/add";
	}

	@GetMapping("/edit/{id}")
	String edit(@PathVariable("id") Long id,Model model){
		ReserveUserOrderDetailsDO reserveUserOrderDetails = reserveUserOrderDetailsService.selectById(id);
		model.addAttribute("reserveUserOrderDetails", reserveUserOrderDetails);
	    return "jzweb/reserveUserOrderDetails/edit";
	}

	
	@Log("修改预定商品用户订单表")
	@ResponseBody
	@RequestMapping("/update")
	public Result<String>  update( ReserveUserOrderDetailsDO reserveUserOrderDetails){
		reserveUserOrderDetails.setUpdateTime(new Date()); //修改时间
		reserveUserOrderDetails.setDeliverDate(new Date()); //发货时间
		reserveUserOrderDetails.setConsigner(this.getUser().getName()); //发货人
		reserveUserOrderDetails.setType("1"); //发货状态
		boolean update = reserveUserOrderDetailsService.updateById(reserveUserOrderDetails);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("取消订单")
	@PostMapping( "/remove")
	@ResponseBody
	public Result<?>  remove( Long id){
		ReserveUserOrderDetailsDO reserveUserOrderDetails = reserveUserOrderDetailsService.selectById(id);
		reserveUserOrderDetails.setType("4"); //type = 4 表示取消订单
		reserveUserOrderDetails.setCancelOrderTime(new Date()); //订单取消时间
        return Result.ok(reserveUserOrderDetailsService.updateById(reserveUserOrderDetails));
	}

}
